package com.light.mytext.mycustomview

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.light.mytext.R
import com.light.mytext.utiles.MiscUtil

class LinePointText(context: Context?/*, attrs: AttributeSet?*/) : View(context/*, attrs*/) {
    var floatX=1000f
    var floatY=2000f


    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        var mPaint = Paint()
        mPaint.color = resources.getColor(R.color.colorAccent)
        mPaint.textSize = 50f
        mPaint.strokeWidth = 3f//画笔宽度
        canvas.drawText("画圆:", 100f, 100f, mPaint)
        //画圆
        mPaint.color = resources.getColor(R.color.c1)
        canvas.drawCircle(250f, 100f, 50f, mPaint)
        mPaint.isAntiAlias = false// 设置画笔的锯齿效果。 true是去除
        //画斜线
       /* mPaint.color = Color.BLACK
        canvas.drawText("画斜线:", 100f, 300f, mPaint)
        canvas.drawLine(100f, 350f, 400f, 500f, mPaint)
        //画直线
        mPaint.color = resources.getColor(R.color.orange)
        canvas.drawText("画直线:", 100f, 450f, mPaint)
        canvas.drawLine(100f, 500f, 400f, 500f, mPaint)
        *//**
         *  画弧线
         * canvas.drawArc(oval, startAngle, sweepAngle, useCenter, paint)
         * 第一个参数oval为RectF类型，即圆弧显示区域，
         * startAngle和sweepAngle均为float类型，分别表示圆弧起始 角度和圆弧度数,3点钟方向为0度，
         * useCenter设置是否显示圆心，boolean类型，paint为画笔；
         *//*
        mPaint.color = resources.getColor(R.color.Color_ff0000)
        canvas.drawText("画弧线:", 100f, 600f, mPaint)
        mPaint.style = Paint.Style.STROKE//设置空心
        var rectF = RectF(100f, 650f, 200f, 700f)
        canvas.drawArc(rectF, 180f, 180f, false, mPaint)
        //画弧线显示中心
        var rectFWithCenter = RectF(250f, 650f, 350f, 700f)
        canvas.drawArc(rectFWithCenter, 180f, 180f, true, mPaint)
        *//**
         * 画正方形
         * canvas.drawRect(100f,800f,200f,900f,mPaint)
         *前四个参数为 left  top  right  bottom
         * 正方形时候 right-left=bottom-top
         *//*
        mPaint.color = resources.getColor(R.color.dainz_color)
        mPaint.style = Paint.Style.FILL//设置充满
        canvas.drawText("画正方形:", 100f, 750f, mPaint)
        canvas.drawRect(100f, 800f, 200f, 900f, mPaint)
        *//** 画长方形
         * canvas.drawRect(100f,800f,200f,900f,mPaint)
         *前四个参数为 left  top  right  bottom
         * 长方形时候 right-left!=bottom-top
         *//*
        mPaint.color = resources.getColor(R.color.unPass_color)
        mPaint.style = Paint.Style.FILL//设置充满
        canvas.drawText("画长方形:", 100f, 950f, mPaint)
        canvas.drawRect(100f, 1000f, 200f, 1200f, mPaint)
        //画扇形
        mPaint.color = resources.getColor(R.color.warn_color)
        mPaint.style = Paint.Style.FILL//设置充满
        canvas.drawText("画扇形:", 100f, 1300f, mPaint)
        *//* 设置渐变色 这个正方形的颜色是改变的 *//*
        val mShader: Shader = LinearGradient(
            0f, 0f, 100f, 100f, intArrayOf(
                Color.RED,
                Color.GREEN,
                Color.BLUE,
                Color.YELLOW,
                Color.LTGRAY
            ), null, Shader.TileMode.REPEAT
        ) // 一个材质,打造出一个线性梯度沿著一条线。
        mPaint.shader = mShader
        val oval2 = RectF(60f, 1400f, 200f, 1540f) // 设置个新的长方形，扫描测量
        canvas.drawArc(oval2, 200f, 130f, true, mPaint)
        //画椭圆，把oval改一下
        oval2.set(210f, 1600f, 250f, 1630f)
        canvas.drawOval(oval2, mPaint);
        mPaint.shader =null
        mPaint.color = resources.getColor(R.color.warn_color)
        canvas.drawText("画三角形：", 100f, 1700f, mPaint)
        // 绘制这个三角形,你可以绘制任意多边形
        val path = Path()
        path.moveTo(80f, 1800f) // 此点为多边形的起点

        path.lineTo(180f, 2000f)
        path.lineTo(80f, 2000f)
        path.close() // 使这些点构成封闭的多边形
        canvas.drawPath(path, mPaint)
        //画光标
        mPaint.style = Paint.Style.STROKE//设置空心
        val pathG = Path()
        pathG.moveTo(150f,2800f)
        pathG.lineTo(0f,2800f)
        pathG.lineTo(0f,2850f)
        pathG.lineTo(150f,2850f)
//        pathG.close() // 使这些点构成封闭的多边形
        canvas.drawPath(pathG, mPaint)
        pathG.reset()
        pathG.moveTo(180f,2770f)
        pathG.lineTo(180f,2620f)
        pathG.lineTo(230f,2620f)
        pathG.lineTo(230f,2770f)
        canvas.drawPath(pathG, mPaint)
        pathG.reset()
        pathG.moveTo(260f,2800f)
        pathG.lineTo(410f,2800f)
        pathG.lineTo(410f,2850f)
        pathG.lineTo(260f,2850f)
        canvas.drawPath(pathG, mPaint)
        pathG.reset()
        pathG.moveTo(230f,2880f)
        pathG.lineTo(230f,3030f)
        pathG.lineTo(180f,3030f)
        pathG.lineTo(180f,2880f)
        canvas.drawPath(pathG, mPaint)
        canvas.drawCircle(205f, 2825f, 60f, mPaint)
        mPaint.isAntiAlias = false// 设置画笔的锯齿效果。 true是去除*/
    }
    var moveX=0
    var moveY=0
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        val x = event!!.x.toInt()
        val y = event.y.toInt()

        when (event!!.action) {
            MotionEvent.ACTION_DOWN -> {
                moveX=x
                moveY=y
            }
            MotionEvent.ACTION_MOVE -> {
                //计算移动的距离
                val offsetX: Int = x - moveX
                val offsetY: Int = y - moveY
                //调用layout方法来重新放置它的位置
                offsetLeftAndRight(offsetX)
                offsetTopAndBottom(offsetY)
            }
        }
        return true
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(
            MiscUtil.measure(widthMeasureSpec, 150),
            MiscUtil.measure(heightMeasureSpec, 150)
        )
    }
}